package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrEnum;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;

public class SubChoiceFrameResp extends AxdrType {
    private byte[] dataCode;
    private AxdrEnum dar;
    private SubSeqOfAttr attr;
    private SubSeqOfRecordAttr recordAttr;
    private Choice choice;

    public SubChoiceFrameResp() {
        choice = Choice._ERR_NONE_SELECTED;
    }

    public void set_dataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setDar(AxdrEnum dar) {
        resetChoices();
        choice = Choice.DAR;
        this.dar = dar;
    }

    public void setAttr(SubSeqOfAttr attr) {
        resetChoices();
        choice = Choice.ATTR;
        this.attr = attr;
    }

    public void setRecordAttr(SubSeqOfRecordAttr recordAttr) {
        resetChoices();
        choice = Choice.RECORD_ATTR;
        this.recordAttr = recordAttr;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();
        choosen.setConst();
        codeLength += choosen.decode(input);
        resetChoices();
        choice = Choice.getEnumByValue((int)choosen.getValue());
        if (choice == Choice.DAR) {
            dar = new AxdrEnum();
            dar.setConst();
            codeLength += dar.decode(input);
            return codeLength;
        } else if (choice == Choice.ATTR) {
            attr = new SubSeqOfAttr();
            codeLength += attr.decode(input);
            return codeLength;
        } else if (choice == Choice.RECORD_ATTR) {
            recordAttr = new SubSeqOfRecordAttr();
            codeLength += recordAttr.decode(input);
            return codeLength;
        }
        throw new Exception("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        if (dataCode != null) {
            for (int i = dataCode.length - 1; i >= 0; i--) {
                output.write(dataCode[i]);
            }
            return dataCode.length;
        }
        if (choice == Choice._ERR_NONE_SELECTED) {
            throw new Exception("Error encoding AxdrChoice: No item in choice was selected.");
        }
        int codeLength = 0;
        if (choice == Choice.RECORD_ATTR) {
            codeLength += recordAttr.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(2);
            codeLength += c.encode(output);
            return codeLength;
        } else if (choice == Choice.ATTR) {
            codeLength += attr.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(1);
            codeLength += c.encode(output);
            return codeLength;
        } else if (choice == Choice.DAR) {
            codeLength += dar.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(0);
            codeLength += c.encode(output);
            return codeLength;
        }
        throw new Exception("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        encode(revOStream);
        dataCode = revOStream.getArray();
    }

    public Choice getChoiceIndex() {
        return choice;
    }

    private void resetChoices() {
        choice = Choice._ERR_NONE_SELECTED;
        dar = null;
        attr = null;
        recordAttr = null;
    }

    public String toString() {
        if (choice == Choice.DAR) {
            if (dar != null) {
                return "choice: {dar: " + dar + "}";
            } else {
                return "choice is dar but dar is null";
            }
        } else if (choice == Choice.ATTR) {
            if (attr != null) {
                return "choice: {attr: " + attr + "}";
            } else {
                return "choice is attr but attr is null";
            }
        } else if (choice == Choice.RECORD_ATTR) {
            if (recordAttr != null) {
                return "choice: {recordAttr: " + recordAttr + "}";
            } else {
                return "choice is recordAttr but recordAttr is null";
            }
        } else {
            return "unknown";
        }
    }

    public enum Choice {
        _ERR_NONE_SELECTED(-1),
        DAR(0),
        ATTR(1),
        RECORD_ATTR(2);

        private final int value;

        Choice(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static Choice getEnumByValue(int value) {
            for (Choice item : Choice.values()) {
                if (value == item.getValue()) {
                    return item;
                }
            }
            return null;
        }
    }
}
